/*---------------------------------------------------------------------------*/
/*!				\file				InitEnd.c
				\date				march 2010
				\author				TNick

				\brief				Initialization and termination code
    
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */
	#include	"Config.h"
	#include	<windows.h>
	#include	<Commctrl.h>
	
	#include	"main.h"
	#include	"Resources.h"
	
/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */

/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */

/*  DATA    ================================================================ */
//
//
//
//
/*  FUNCTIONS    ----------------------------------------------------------- */


/* ------------------------------------------------------------------------- */
unsigned		App_Init		(void)
{
    WNDCLASSEX	wincl;        // Data structure for the windowclass 
    DWORD		ExFl = WSEX_MAIN_F;
	unsigned	i;
	
	// get heap handle for all allocations made by this library
	hHeapApp = GetProcessHeap();
	
	// cache handle of calling application
	hInstance = GetModuleHandle(NULL);
	
    // we will use some common controls
    InitCommonControls();
	
	// options are not shown
	hWOpts = NULL;
	
	// load default values
	AppStgs.ShowSrg.Letter = 'W';
	AppStgs.ShowSrg.Ctrl = 1;
	AppStgs.ShowSrg.Alt = 0;
	AppStgs.ShowSrg.Shift = 0;
	AppStgs.HideSrg.Letter = 'Q';
	AppStgs.HideSrg.Ctrl = 1;
	AppStgs.HideSrg.Alt = 0;
	AppStgs.HideSrg.Shift = 0;
	AppStgs.ShowGest[0] = 1;
	AppStgs.ShowGest[1] = 0;
	AppStgs.ShowGest[2] = 1;
	AppStgs.ShowGest[3] = 0;
	AppStgs.HideGest[0] = 1;
	AppStgs.HideGest[1] = 1;
	AppStgs.HideGest[2] = 0;
	AppStgs.HideGest[3] = 0;
	AppStgs.EpocsToWait = 4;
	AppStgs.Flags = APPFL_FLTR_NAME | APPFL_FLTR_ICN;
	AppStgs.LsL_ColW[0] = 50;
	AppStgs.LsR_ColW[0] = 50;
	AppStgs.Width_Left = 240;
	AppStgs.rMW.left = 100;
	AppStgs.rMW.top = 100;
	AppStgs.rMW.right = 640;
	AppStgs.rMW.bottom = 480;
	
	for (i = 1; i < LISTCOLS_CNT; i++)
	{
		AppStgs.LsL_ColW[i] = 150;
		AppStgs.LsR_ColW[i] = 150;
	}
	
	// then try to load previous settings
	GetSettings(&AppStgs);
    
    // fonts and other graphics objects
    hF_Gen = (HFONT)GetStockObject(ANSI_VAR_FONT);
    hImLst = ImageList_LoadImage(
				hInstance,
				MAKEINTRESOURCE(ID_BITMAP_EXC),
				SIZE_IMGINRES,SIZE_IMGINRES,
				0xFFFFFF,IMAGE_BITMAP,LR_DEFAULTCOLOR);
    
    // screen infos (usefull with mouse gestures)
    cxScreen = GetSystemMetrics(SM_CXSCREEN);
    cyScreen = GetSystemMetrics(SM_CYSCREEN);
    
    // direct insight in input stream
	g_keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, &KeyboardCallback, hInstance, 0);
	g_mouseHook = SetWindowsHookEx(WH_MOUSE_LL, &MouseProc, hInstance, 0);
    
    wincl.hInstance = hInstance;
    wincl.lpszClassName = CLASSNAME_MAIN;
    wincl.lpfnWndProc = WP_Main;
    wincl.style = CS_BYTEALIGNCLIENT | CS_BYTEALIGNWINDOW;
    wincl.cbSize = sizeof (WNDCLASSEX);

    wincl.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE(ID_ICON_MAIN));
    wincl.hIconSm = wincl.hIcon;
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = MAKEINTRESOURCE(IDM_MAINAPP);
    wincl.cbClsExtra = 0;
    wincl.cbWndExtra = 0;
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    if (!RegisterClassEx (&wincl)) return 0;

    wincl.lpfnWndProc = WP_Div;//splitter (div)
    wincl.lpszMenuName = NULL;
    wincl.lpszClassName = CALSS_DIV;
	wincl.hCursor = LoadCursor (NULL, IDC_SIZEWE);
    wincl.hbrBackground = (HBRUSH)COLOR_INACTIVEBORDER+1;
    if (!RegisterClassEx (&wincl)) return 0;

    // The class is registered, let's create the window
    if (AppStgs.Flags & APPFL_ONTOP) ExFl |= WS_EX_TOPMOST;
    hwMain = CreateWindowEx (
			ExFl,
			CLASSNAME_MAIN,
			APPNAME_START, 
			WS_MAIN_F,
			AppStgs.rMW.left,
			AppStgs.rMW.top,
			AppStgs.rMW.right,
			AppStgs.rMW.bottom,
			HWND_DESKTOP,
			NULL,                // menu 
			hInstance,           // Program Instance handler 
			NULL                 // No Window Creation data 
			);
    if (hwMain == 0) return 0;
	

    // Make the window visible on the screen 
    if (!(AppStgs.Flags & APPFL_STARTHD))
		ShowWindow (hwMain, SW_SHOW);

	// initialization ok
	return TRUE;
}    
/* ========================================================================= */


/* ------------------------------------------------------------------------- */
void			App_End			(void)
{
	UnhookWindowsHookEx(g_keyboardHook);
	UnhookWindowsHookEx(g_mouseHook);

    // fonts
    DeleteObject(hF_Gen);
    
    // unregister classes used by application
    UnregisterClass(CLASSNAME_MAIN,hInstance);
	
}
/* ========================================================================= */


/*  FUNCTIONS    =========================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
